package com.project.mq.demo;


import com.project.mq.common.MqException;
import com.project.mq.mqclient.Channel;
import com.project.mq.mqclient.Connection;
import com.project.mq.mqclient.ConnectionFactory;
import com.project.mq.mqserver.core.ExchangeType;

import java.io.IOException;

/**
 *这个类用来表示一个生产者。
 * 通常这是一个单独的服务器程序。
 */
public class DemoProducer {
    public static void main(String[] args) throws IOException, MqException, InterruptedException {
        System.out.println("启动生产者!");

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(9090);
        //factory.setUsername("admin");
        //factory.setPassword("admin");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("testExchange", ExchangeType.DIRECT, true, false, null);
        channel.queueDeclare("testQueue", true, false, false, null);

        byte[] body = "hello".getBytes();
        boolean ok = channel.basicPublish("testExchange", "testQueue", null, body);
        System.out.println("投递消息完成! ok=" + ok);

        Thread.sleep(500);
        channel.close();
        connection.close();
    }
}